package managers.dateManager.endFrozen.durationChange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.DateManagerException;
import org.tbjd.client.managers.TaskManager;
import org.tbjd.client.states.StateBeginFrozenSPEC;
import org.tbjd.client.states.StateEndFrozenSPEC;

import factories.TaskFactory;

/**
 * <p>
 * Test TaskManager's methods
 * </p>
 * <p>
 * <b>Case</b>
 * </p>
 * <p>
 * A task with multiple predecessors and successors as well as isolated tasks
 * </p>
 * <p>
 * <b>Action</b>
 * </p>
 * <p>
 * Duration of the task "task" is changed
 * </p>
 * <b>Expected</b>
 * <ul>
 * <li>update BeginDate of "task"</li>
 * <li>Switch "task" to manual Chaining mode</li>
 * </ul>
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class EndFrozen_DurationChange_PredecessorsAndSuccessors_Basic extends TestCase {

	/*
	 * Task		Begins 		Ends 		Successor 	ChainingMode	duration + 24 hours
	 * p1		1.1.2009 	2.1.2009 	task		manual 			1.1.2009 - 2.1.2009
	 * t1		1.5.2009 	3.5.2009 	-			manual 			1.5.2009 - 3.5.2009
	 * task 	4.1.2009 	5.1.2009 	s1			manual 			3.1.2009 - 5.1.2009
	 * t2 		1.1.2009 	3.1.2009 	-			manual 			1.1.2009 - 3.1.2009
	 * s1	 	11.1.2009 	14.1.2009 	-			manual 			11.1.2009 - 14.1.2009
	 */

	private Map<Integer, Object> map = null;
	private Logger log = Logger.getLogger("console");
	private DateFormat df = DateFormat.getInstance();
	private Task task = null;
	private TaskManager dateManager = new TaskManager();

	protected void setUp() throws Exception {
		df = new SimpleDateFormat("dd.MM.yyyy");

		Task p1 = TaskFactory.getInstance();
		p1.setBeginDate(df.parse("1.1.2009"));
		p1.setEndDate(df.parse("2.1.2009"));
		p1.setDuration(24f);
		p1.setState(new StateBeginFrozenSPEC(p1.getState().getDurationModeState()));

		Task t1 = TaskFactory.getInstance();
		t1.setBeginDate(df.parse("1.5.2009"));
		t1.setEndDate(df.parse("3.5.2009"));
		t1.setDuration(48f);
		t1.setState(new StateBeginFrozenSPEC(t1.getState().getDurationModeState()));

		this.task = TaskFactory.getInstance();
		this.task.setName("task");
		this.task.setBeginDate(df.parse("4.1.2009"));
		this.task.setEndDate(df.parse("5.1.2009"));
		this.task.setDuration(24f);
		this.task.setState(new StateEndFrozenSPEC(task.getState().getDurationModeState()));

		Task t2 = TaskFactory.getInstance();
		t2.setBeginDate(df.parse("1.1.2009"));
		t2.setEndDate(df.parse("3.1.2009"));
		t2.setDuration(48f);
		t2.setState(new StateBeginFrozenSPEC(t2.getState().getDurationModeState()));

		Task s1 = TaskFactory.getInstance();
		s1.setBeginDate(df.parse("11.1.2009"));
		s1.setEndDate(df.parse("14.1.2009"));
		s1.setDuration(72f);
		s1.setState(new StateBeginFrozenSPEC(s1.getState().getDurationModeState()));

		p1.addSuccessor(this.task);
		this.task.addSuccessor(s1);

		super.setUp();
	}

	public void testReturnedBeanListNotNull() {
		log.debug(" === testReturnedBeanListNotNull");

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call dateManager...");
		try {
			assertNotNull("Updated bean list isn't null", dateManager.manageDates(map));

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}

		System.out.println("");
	}

	public void testListSize() {
		log.debug(" === testListSize");

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}
		assertTrue("Updated bean list size = 1", updatedBeans.size() == 1);

		System.out.println("");
	}

	public void testInstanceEquality() {
		log.debug(" === testInstanceEquality");

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}

		assertEquals("Task given and received are equals", this.task, updatedBeans.get(0));

		System.out.println("");
	}

	public void testDates() {
		log.debug(" === testDatesAreOk");
		log.debug("original endDate = " + this.task.getEndDate());

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}

		for (Task updatedBean : updatedBeans) {
			log.debug(updatedBean.getName() + " \t updated with BeginDate = " + df.format(updatedBean.getBeginDate()) + " , EndDate = "
					+ df.format(updatedBean.getEndDate()) + " " + ((updatedBean.getState().isASAPChainingMode()) ? "ASAP" : "Manually"));

			try {
				if (updatedBean.getName().equalsIgnoreCase("p1")) {
					fail("p1 must not change");
				}
				if (updatedBean.getName().equalsIgnoreCase("t1")) {
					fail("t1 must not change");
				}
				if (updatedBean.getName().equalsIgnoreCase("task")) {
					assertEquals("check task BeginDate", df.parseObject("3.1.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("5.1.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("t2")) {
					fail("t2 must not change");
				}
				if (updatedBean.getName().equalsIgnoreCase("s1")) {
					fail("s1 must not change");
				}
			} catch (ParseException e) {
				fail(e.getMessage());

			}
		}

		System.out.println("");
	}

	public void testDurations() {
		log.debug(" === testDurations");

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("compute with new Duration = " + newDuration + "...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());
			
		}

		assertEquals(48f, updatedBeans.get(0).getDuration());

		System.out.println("");
	}

	public void testRevertDates() {
		log.debug(" === testRevertDates");

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		try {
			dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager with original BeginDate...");

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, originalDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}

		log.debug("retrieved BeginDate = " + updatedBeans.get(0).getBeginDate() + " & original BeginDate = " + originalBeginDate);
		assertEquals(originalBeginDate, updatedBeans.get(0).getBeginDate());
		assertEquals(originalEndDate, updatedBeans.get(0).getEndDate());

		System.out.println("");
	}

	public void testRevertDurations() {
		log.debug(" === testRevertDurations");

		float originalDuration = this.task.getDuration();

		float newDuration = 48f;
		log.debug("new duration = " + newDuration);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, newDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("original duration = " + originalDuration);

		log.debug("call dateManager with new duration = " + newDuration + "...");
		try {
			dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager with original duration = " + originalDuration + "...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_duration, originalDuration);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager .manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());
		}

		assertEquals("Assert Duration reverted to original", originalDuration, updatedBeans.get(0).getDuration());

		System.out.println("");
	}

}